<template>
	<view>
		<text>我的</text>
		<button type="default" @click="handleLogin">登录</button>
		
		<button type="default" open-type="chooseAvatar" @chooseavatar="handleChoose">选择头像</button>
		<input type="nickname"  v-model="nickname">
		<button type="default" @click="handleSubmit">确定</button>
		<image :src="avatarUrl" mode=""></image>
	</view>
</template>

<script>
	import {url} from '../../services/index.js';
	import {login,fillDetail} from '../../services/user.js'
	export default {
		data() {
			return {
				image:"",
				avatarUrl:"",
				nickname:"",
				user:{}
			}
		},
		methods: {
			handleLogin(){
				uni.login({
				  provider: 'weixin',
				  success: async (loginRes) => {
				    console.log("loginRes:",loginRes);
					let res = await login(loginRes.code);
					
					
					this.user = res.data[0];
					this.avatarUrl = url + '/images/' + res.data[0].headImg;
					this.nickname = res.data[0].nickname;
				
					
				  }
				});
			},
			async handleChoose(e){
				console.log(e.detail.avatarUrl);
				// 将选择的图片转换成base64编码
				this.image = wx.getFileSystemManager().readFileSync(e.detail.avatarUrl,'base64')
				this.avatarUrl = e.detail.avatarUrl
	
				
				// console.log("data",data);
			},
			async handleSubmit(){
				console.log("this.user",this.user._id);
				let data = await fillDetail({
					id:this.user._id,
					image:this.image,
					nickname:this.nickname
				});
		// 		uni.setStorage({
		// 			key: 'user',
		// 			data: user
		
		// 		});
				// console.log(this.image);
			}
		}
	}
</script>

<style>

</style>
